<template>
	<view class="container" style="min-height:100vh">
		<view class="head" :style="{ backgroundImage: `url(${staticFileDomain}/static/image/background-gift-head.png)` }">
			<view class="head-user">
				<image class="head-user-face" :src="userInfo.avatar.file_path"></image>
				<view class="head-user-text">{{ userInfo.nickName }}</view>
			</view>
		</view>
		<view class="gift-card" :style="{ backgroundImage: `url(${staticFileDomain}/static/image/background-gift-card.png)` }">
			<view class="gift-card-title">礼物金额</view>
			<view class="gift-card-money">{{ total_amount - settled_amount }}</view>
			<view class="gift-card-bottom">
				<view class="gift-card-history">¥{{ settled_amount }}</view>
				<view class="gift-card-btn" @tap.stop="askWithdraw" :class="{ disable: !(total_amount - settled_amount > 0) }">全部转入钱包</view>
			</view>
		</view>
		<u-sticky bg-color="#ffffff" :offset-top="offsetTop" z-index="99">
			<view class="gift-nav">
				<view class="gift-nav-item " :data-type="1" @tap.stop="switchNav" :class="{ checked: switchType == 1 }">收到礼物</view>
				<view class="gift-nav-item" :data-type="2" @tap.stop="switchNav" :class="{ checked: switchType == 2 }">送出礼物</view>
			</view>
		</u-sticky>
		<view class="gift-body">
			<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback">
				<!--  -->
				<view class="gift-list">
					<template v-if="switchType == 1">
						<view class="gift-item" v-for="item in receiveList" :key="item.order_no">
							<image class="gift-face" :src="item.giver.avatar"></image>
							<view class="gift-from">
								<view class="gift-from-name">{{ item.giver.nickName }}</view>
								<view class="gift-from-time">{{ item.create_time }}</view>
							</view>
							<view class="gift-desc">
								<view class="gift-desc-gift">
									<text>赠送我</text>
									{{ item.orderPlan.gift_name }}
								</view>
								<view class="gift-desc-price">+{{ item.orderPlan.money }}</view>
							</view>
						</view>
					</template>
					<template v-if="switchType == 2">
						<view class="gift-item" v-for="item in givesList" :key="item.order_no">
							<image class="gift-face" :src="item.receiver.avatar"></image>
							<view class="gift-from">
								<view class="gift-from-name">{{ item.receiver.nickName }}</view>
								<view class="gift-from-time">{{ item.create_time }}</view>
							</view>
							<view class="gift-desc">
								<view class="gift-desc-gift">
									<text>赠送TA</text>
									{{ item.orderPlan.gift_name }}
								</view>
								<view class="gift-desc-price">+{{ item.orderPlan.money }}</view>
							</view>
						</view>
					</template>
				</view>
			</mescroll-body>
		</view>
	</view>
</template>

<script>
import Tools from '@/utils/Tools.js';
import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
export default {
	mixins: [MescrollMixin],
	data() {
		return {
			offsetTop: 0,
			settled_amount: 0,
			total_amount: 0,
			givesList: [],
			receiveList: [],
			switchType: 1,
			gift_rate: 0,
			disable: false
		};
	},
	onLoad() {
		const systemInfo = uni.getSystemInfoSync();
		// #ifdef APP-PLUS
		this.offsetTop = Tools.px2rpx(44) + systemInfo.statusBarHeight;
		// #endif
	},
	onShow() {
		this.getGiftCenter();
	},
	methods: {
		getGiftCenter() {
			this.$u.api.getGiftCenter().then(res => {
				this.settled_amount = res.data.settled_amount;
				this.total_amount = res.data.total_amount;
				this.gift_rate = res.data.gift_rate;
			});
		},
		switchNav(e) {
			this.switchType = e.currentTarget.dataset.type;
			uni.pageScrollTo({
				scrollTop: 0,
				duration: 300
			});
			this.mescroll.resetUpScroll(true);
		},
		upCallback(mescroll) {
			if (this.switchType == 1) {
				this.getGiftReceiveList(mescroll);
			} else {
				this.getGiftGivesList(mescroll);
			}

			// this.$u.api.getGiftReceiveList().then(res=>{
			// 	console.log(res)
			// })
			// getGiftGivesList
		},
		getGiftGivesList(mescroll) {
			this.$u.api.getGiftGivesList(mescroll.num).then(res => {
				const listData = res.data.list;
				if (mescroll.num == 1) {
					this.givesList = listData.data;
				} else {
					this.givesList.push(...listData.data);
				}
				this.mescroll.endBySize(listData.data.length, listData.total);
			});
		},
		getGiftReceiveList(mescroll) {
			this.$u.api.getGiftReceiveList(0, mescroll.num).then(res => {
				const listData = res.data.list;
				if (mescroll.num == 1) {
					this.receiveList = listData.data;
				} else {
					this.receiveList.push(...listData.data);
				}
				this.mescroll.endBySize(listData.data.length, listData.total);
			});
		},
		askWithdraw() {
			if (!this.disable && this.total_amount - this.settled_amount >0) {
				uni.showModal({
					title: '转入钱包',
					content: `本次转入钱包需要扣除手续费${this.gift_rate}%,\n确认是否全部转入钱包`,
					confirmText: '全部转入',
					cancelText: '让我想想',
					success: res => {
						if (res.confirm) {
							this.toWithdraw();
						}
					}
				});
			}
		},
		toWithdraw() {
			this.$u.api
				.submitGiftSettle()
				.then(res => {
					this.getGiftCenter();
					uni.showModal({
						title: '提示',
						content: '礼物转入到钱包成功',
						success: res => {
							this.navigateBack();
						}
					});
				})
				.catch(e => {
					this.disable = false;
				});
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #fff;
}
.head {
	width: 750rpx;
	/* #ifdef MP */
	height: 280rpx;
	padding: 6rpx 0 0;
	/* #endif */
	/* #ifndef MP */
	height: calc(280rpx + 44px + var(--status-bar-height));
	padding: calc(44px + var(--status-bar-height)) 0 0;
	/* #endif */
	background-position: center bottom;
	background-size: cover;
	display: flex;
	flex-direction: column;
	align-items: center;
	overflow: visible;
	.head-user {
		padding: 0 75rpx;
		height: 100rpx;
		display: flex;
		align-items: center;
		width: 100%;
		.head-user-face {
			height: 100rpx;
			width: 100rpx;
			border-radius: 100rpx;
		}
		.head-user-text {
			font-size: 40rpx;
			color: #ffffff;
			margin-left: 25rpx;
		}
	}
}
.gift-card {
	width: 696rpx;
	height: 277rpx;
	background-size: cover;
	margin: -158rpx auto 0;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	padding: 38rpx 47rpx 44rpx 63rpx;
	.gift-card-title {
		height: 36rpx;
		font-weight: 400;
		font-size: 26rpx;
		color: rgba(0, 0, 0, 0.85);
		display: flex;
		align-items: center;
		&::after {
			content: 'Total';
			color: #6d7278;
			font-size: 24rpx;
			margin-left: 6rpx;
		}
	}
	.gift-card-money {
		font-size: 60rpx;
		height: 84rpx;
		display: flex;
		align-items: flex-end;
		&::before {
			content: '¥';
			font-size: 38rpx;
			margin-right: 4rpx;
			padding-bottom: 10rpx;
		}
	}
	.gift-card-bottom {
		display: flex;
		justify-content: space-between;
		align-items: center;
		.gift-card-history {
			height: 36rpx;
			display: flex;
			align-items: center;
			color: #979797;
			font-size: 28rpx;
			&::before {
				content: '已提现：';
				font-size: 22rpx;
				font-weight: 400;
				margin-right: 2rpx;
				color: rgba(0, 0, 0, 0.85);
			}
		}
		.gift-card-btn {
			height: 46rpx;
			background: linear-gradient(180deg, #ed4f5d 0%, #fb7a8e 100%);
			border-radius: 23rpx;
			padding: 16rpx;
			display: flex;
			align-items: center;
			color: #fff;
			font-size: 24rpx;
			font-weight: 400;
			&.disable{
				opacity: .6;
			}
		}
	}
}
.gift-nav {
	height: 110rpx;
	display: flex;
	background-color: #fff;
	.gift-nav-item {
		flex: 1;
		position: relative;
		padding: 30rpx 0;
		display: flex;
		align-items: center;
		justify-content: center;

		font-size: 28rpx;
		font-weight: 400;
		color: rgba(0, 0, 0, 0.85);
		&.checked {
			&::after {
				content: '';
				width: 60rpx;
				height: 8rpx;
				background: #f85962;
				border-radius: 4rpx;
				position: absolute;
				left: 50%;
				top: 76rpx;
				transform: translateX(-50%);
			}
		}
	}
}
.gift-body {
	.gift-item {
		padding: 25rpx 40rpx;
		display: flex;
		.gift-face {
			height: 90rpx;
			width: 90rpx;
			border-radius: 45rpx;
			flex-shrink: 0;
		}
		.gift-from {
			height: 90rpx;
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			padding: 3rpx 0 4rpx 20rpx;
			.gift-from-name {
				font-size: 28rpx;
				font-weight: 400;
				color: rgba(0, 0, 0, 0.85);
			}
			.gift-from-time {
				font-size: 22rpx;
				font-weight: 400;
				color: #979797;
			}
		}
		.gift-desc {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			align-items: flex-end;
			padding: 3rpx 0 4rpx 20rpx;
			.gift-desc-gift {
				font-size: 30rpx;
				font-weight: 400;
				text {
					font-size: 26rpx;
					padding-right: 6rpx;
				}
			}
			.gift-desc-price {
				font-size: 24rpx;
				font-weight: 500;
				color: #f85962;
			}
		}
		// .gift-item
	}
}
</style>
